package com.file.manage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class TimeUtils {
	
	private static String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
	
    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    public static final SimpleDateFormat DATE_FORMAT_DATE    = new SimpleDateFormat("yyyy-MM-dd");
    
    public static final SimpleDateFormat DATE_FORMAT_DATE_ZH_CN    = new SimpleDateFormat("yyyy年MM月dd日");

    /**
     * long time to string
     * 
     * @param timeInMillis
     * @param dateFormat
     * @return
     */
    public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }

    /**
     * long time to string, format is {@link #DEFAULT_DATE_FORMAT}
     * 
     * @param timeInMillis
     * @return
     */
    public static String getTime(long timeInMillis) {
        return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
    }

    /**
     * get current time in milliseconds
     * 
     * @return
     */
    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    /**
     * get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT}
     * 
     * @return
     */
    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    /**
     * get current time in milliseconds
     * 
     * @return
     */
    public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
        return getTime(getCurrentTimeInLong(), dateFormat);
    }
    
    /**
	 * @Title: getCurrentDateString
	 * @Description: 获取当前时间
	 * @param paramString ：时间格式——yyyy-MM-dd HH:mm:ss
	 * @return
	 * @return String
	 */
	public static String getCurrentDateString() {
		return DATE_FORMAT_DATE.format(new Date()).toString();
	}
	
	/** 
	* @Title: getWeekDay 
	* @Description: 获取周 
	* @param dateStr : 2000-09-08
	* @return
	* @return String 
	*/ 
	public static String getWeekDay(String dateStr){
		Date dt = null;
		try {
			dt = DATE_FORMAT_DATE_ZH_CN.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
	}
	
	 /**
     * 获得指定日期的前一天
     * 
     * @param specifiedDay:2000-09-08
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = DATE_FORMAT_DATE.parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);
        String dayBefore = DATE_FORMAT_DATE.format(c
                .getTime());
        return dayBefore;
    }


	
	/** 
	* @Title: getSpecifiedDayAfter 
	* @Description: 获取后一天
	* @param specifiedDay
	* @return
	* @return String 
	*/ 
	public static String getSpecifiedDayAfter(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = DATE_FORMAT_DATE_ZH_CN.parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayAfter = DATE_FORMAT_DATE_ZH_CN.format(c.getTime());
        return dayAfter;
    }

	public static String date2FormatDateString(Date paramDate) {
		return DEFAULT_DATE_FORMAT.format(paramDate);
	}

	/** 
	* @Title: getUpdateTime 
	* @Description: 获取更新的时间 
	* @param paramString
	* @return String
	* @throws 
	*/ 
	public static String getUpdateTime(String paramString) {
		String str = "";
		if ((paramString == null) || ("".equals(paramString))) {
			str = "";
			return str;
		}
		Date localDate;
		try {
			localDate = DEFAULT_DATE_FORMAT.parse(paramString);
			long result = (DEFAULT_DATE_FORMAT.parse(
					getCurrentDateString()).getTime() - localDate
					.getTime()) / 1000L;
			long day = result / 86400L;
			long hour = result % 86400L / 3600L;
			long min = result % 3600L / 60L;
			long second = result % 60L / 60L;
			if (day >= 1L)
				str = DATE_FORMAT_DATE.format(localDate);
			if ((hour > 0L) && (day == 0L))
				str = hour + "小时前";
			if ((min > 0L) && (hour == 0L) && (day == 0L))
				str = min + "分钟前";
			str = "1分钟前";
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	
	/** 
	* @Title: isTimeGreateThan18 
	* @Description : 判断当前时间是否在六点之后 
	* @return
	* boolean
	*/ 
	public static boolean isTimeGreateThan18() {
		Date date = new Date(System.currentTimeMillis());
		int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		if (hour >= 18) {
			return true;
		}
		return false;
	}
}
